﻿using Common.Base;
using Common.Enums;

using GameCommon.Base;
using GameCommon.ComponentInterfaces;
using GameCommon.Components;
using GameCommon.Manager;
using GameCommon.ManagerInterfaces;

using GlobalServices = GameCommon.Services.Service;
using Xna = Microsoft.Xna.Framework;

namespace GameGraphics.Components
{
    internal class Text : GameComponent, ITextHitBox
    {
        private string value;

        private HorizontalAlignment horizontalAlignment;

        private VerticalAlignment verticalAlignment;

        private Offset offset;

        public Text(string id, FontSizeEnum fontSize, FontEnum fontEnum)
            : base(id)
        {
            this.Font = Managers.FontManager.GetFont(fontEnum, fontSize);
            this.Dimension = new Dimension();
            this.offset = new Offset();
        }

        public Text(string id, FontSizeEnum fontSize, FontEnum fontEnum, GameComponent parent)
            : base(id)
        {
            this.Parent = parent;
            this.Font = Managers.FontManager.GetFont(fontEnum, fontSize);
            this.Dimension = new Dimension();
            this.offset = new Offset();
        }

        public IHitBox HitBox { get; set; }

        public ToolTip ToolTip { get; set; }

        public HorizontalAlignment HorizontalAlignment
        {
            get
            {
                return this.horizontalAlignment;
            }

            set
            {
                this.horizontalAlignment = value;
                this.ApplyAlignment();
            }
        }

        public bool Selected { get; set; }

        public Font Font { get; private set; }

        public string Value
        {
            get
            {
                return this.value;
            }

            set
            {
                this.value = value;
                this.Dimension.Size = GlobalServices.FontService.GetSize(this.Value, this.Font);
                this.ApplyAlignment();
            }
        }

        public GameComponent Parent { get; private set; }

        public VerticalAlignment VerticalAlignment
        {
            get
            {
                return this.verticalAlignment;
            }

            set
            {
                this.verticalAlignment = value;
                this.ApplyAlignment();
            }
        }

        public new Offset Offset
        {
            get
            {
                return this.offset;
            }

            set
            {
                this.offset = value;
                this.ApplyAlignment();
            }
        }

        public override void Draw(IDrawingManager manager, Xna.GameTime gameTime)
        {
            if (this.ToolTip != null && this.HitBox.IsMouseOver)
            {
                this.ToolTip.Draw(manager, gameTime);
            }

            this.DrawingService.DrawFont(manager, this.Font, this.Dimension, this.Value, this.Opacity, this.Layer);
        }
        
        private void ApplyAlignment()
        {
            if (this.Parent != null)
            {
                GlobalServices.ComponentModifier.ApplyAlignments(this.Dimension, this.Parent.Dimension, this.offset, this.horizontalAlignment, this.verticalAlignment);
            }
        }

        public override string ToString()
        {
            return string.Format("Text, Id: {0}", this.Id);
        }
    }
}